<template>
    <div>
        <a-modal
            :title="modaObj.title"
            :visible="addAndEditVisible"
            width="80%"
            :maskClosable="false"
            :confirm-loading="confirmLoading"
            @ok="handleOk"
            @cancel="handleCancel"
            >
            <a-form-model
                v-if="modaObj.type == 1"
                ref="ruleForm"
                :model="form"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol"
            >
                <!-- <a-form-model-item label="版本">
                    <a-input v-model="form.fbb"/>
                </a-form-model-item> -->
                <a-form-model-item label="法规简称" prop="ffgjc">
                    <a-input v-model="form.ffgjc"/>
                </a-form-model-item>
                <a-form-model-item label="法规名称" prop="ffgmc">
                    <a-input v-model="form.ffgmc" type="textarea" />
                </a-form-model-item>
            </a-form-model>
            <a-form-model
                v-else-if="modaObj.type == 2 || modaObj.type == 3"
                ref="ruleForm"
                :model="form"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol"
            >
                <a-form-model-item label="序号">
                    <a-input v-model="form.forder"/>
                </a-form-model-item>
                <a-form-model-item label="章节简称" prop="fzjjc">
                    <a-input v-model="form.fzjjc"/>
                </a-form-model-item>
                <a-form-model-item label="章节名称" prop="fzjmc">
                    <a-input v-model="form.fzjmc" type="textarea" />
                </a-form-model-item>
            </a-form-model>
            <a-form-model
                v-else-if="modaObj.type == 4"
                ref="ruleForm"
                :model="form"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol"
            >
                <a-form-model-item label="审评要点" prop="fjcyd">
                    <a-input v-model="form.fjcyd" type="textarea"/>
                </a-form-model-item>
                <a-form-model-item label="指导原则号" prop="fzdyzh">
                    <a-input v-model="form.fzdyzh" type="textarea" />
                </a-form-model-item>
                <a-form-model-item label="指导原则内容" prop="fzdyznr">
                    <a-textarea v-model="form.fzdyznr" :autosize="{ minRows: 8, maxRows: 10000 }"/>
                </a-form-model-item>
            </a-form-model>
            <a-form-model
                v-else-if="modaObj.type == 5"
                ref="ruleForm"
                :model="form"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol"
            >
                <a-form-model-item label="来源" prop="fly">
                    <a-input v-model="form.fly"/>
                </a-form-model-item>
                <a-form-model-item label="描述模版" prop="fmsmb">
                    <a-textarea v-model="form.fmsmb" :autosize="{ minRows: 3, maxRows: 10000 }"/>
                </a-form-model-item>
                <a-form-model-item label="使用次数" prop="fsycs">
                    <a-input v-model="form.fsycs" type="number" />
                </a-form-model-item>
            </a-form-model>
        </a-modal>
    </div>
</template>
<script>
import {addFgmc,addZjml,addZjmlInSub,addTk,addMsmb,updateTk,updateMsmb,updateZjml,updateFgmc} from './service/index'
export default {
    props: {
        modaObj: {
            type: Object,
            default: false,
        }
    },
    data() {
        return {
            addAndEditVisible: false,
            confirmLoading: false,
            labelCol: { span: 6 },
            wrapperCol: { span: 14 },
            form: {
                // --1
                fbb: '',
                ffgjc: '',
                ffgmc: '',
                // --
                forder: '',
                fzjjc: '',
                fzjmc: '',
                // ---4
                fjcyd: '',
                fzdyzh: '',
                fzdyznr: '',
                // ---5
                fly: '',
                fmsmb: '',
                fsycs: '',
            },
            kongForm: {},
            rules: {
                ffgjc: [
                    { required: true, message: '请输入法规简称', trigger: 'blur' },
                ],
                ffgmc: [
                    { required: true, message: '请输入法规名称', trigger: 'blur' },
                ],
                // ---2.3
                fzjjc: [
                    { required: true, message: '请输入章节简称', trigger: 'blur' },
                ],
                fzjmc: [
                    { required: true, message: '请输入章节名称', trigger: 'blur' },
                ],
                // ---4
                fjcyd: [
                    { required: true, message: '请输入审评要点', trigger: 'blur' },
                ],
                fzdyzh: [
                    { required: true, message: '请输入指导原则号', trigger: 'blur' },
                ],
                fzdyznr: [
                    { required: true, message: '请输入指导原则内容', trigger: 'blur' },
                ],
                // ---5
                fly: [
                    { required: true, message: '请输入来源', trigger: 'blur' },
                ],
                fmsmb: [
                    { required: true, message: '请输入描述模版', trigger: 'blur' },
                ],
                fsycs: [
                    { required: true, message: '请输入使用次数', trigger: 'blur' },
                ],
            },
            type: [
                {
                    'num':1,'type':'法规名称',
                },{
                    'num':2,'type':'章节名称',
                },{
                    'num':3,'type':'章节-下级',
                },{
                    'num':4,'type':'条款',
                },
            ]
        }
    },
    methods: {
        handleOk(){
            //console.log(this.modaObj)
            this.$refs.ruleForm.validate(valid => {
                //console.log(valid)
                if (valid) {
                    if(this.modaObj.type == 1){
                        if(this.modaObj.formData){
                            let data = {
                                ffgjc: this.form.ffgjc,
                                ffgmc: this.form.ffgmc,
                                id: this.modaObj.formData.id
                            }
                            updateFgmc(data).then(res => {
                                if(res.code == 200){
                                    this.$message.success(res.message)
                                    this.handleCancel()
                                    this.$emit('getData',this.modaObj.fzdwjid)
                                } else {
                                    this.$message.warning(res.message)
                                }
                            })
                        } else {
                            let data = {
                                ffgjc: this.form.ffgjc,
                                ffgmc: this.form.ffgmc,
                                fzdwjid: this.modaObj.fzdwjid,
                            }
                            addFgmc(data).then(res => {
                                if(res.code == 200){
                                    this.$message.success(res.message)
                                    this.handleCancel()
                                    this.$emit('getData',this.modaObj.fzdwjid)
                                } else {
                                    this.$message.warning(res.message)
                                }
                            })
                        }
                    } else if(this.modaObj.type == 2){
                        let data = {
                            forder: this.form.forder,
                            fzjjc: this.form.fzjjc,
                            fzjmc: this.form.fzjmc,
                            ffgid: this.modaObj.ffgid,
                            id: this.modaObj.formData?this.modaObj.id:null
                        }
                        if(this.modaObj.formData){
                            updateZjml(data).then(res => {
                                if(res.code == 200){
                                    this.$message.success(res.message)
                                    this.handleCancel()
                                    this.$emit('getTreeData',this.modaObj.ffgid)
                                } else {
                                    this.$message.warning(res.message)
                                }
                            })
                        } else {
                            addZjml(data).then(res => {
                                if(res.code == 200){
                                    this.$message.success(res.message)
                                    this.handleCancel()
                                    this.$emit('getTreeData',this.modaObj.ffgid)
                                } else {
                                    this.$message.warning(res.message)
                                }
                            })
                        }
                    } else if(this.modaObj.type == 3) {
                        let data = {
                            fzjjc: this.form.fzjjc,
                            fzjmc: this.form.fzjmc,
                            ffgid: this.modaObj.ffgid,
                            ffzjid: this.modaObj.ffzjid,
                        }
                        addZjmlInSub(data).then(res => {
                            if(res.code == 200){
                                this.$message.success(res.message)
                                this.handleCancel()
                                this.$emit('getTreeData',this.modaObj.ffgid)
                            } else {
                                this.$message.warning(res.message)
                            }
                        })
                    } else if(this.modaObj.type == 4){
                        if(this.modaObj.formData){
                            let data = {
                                fjcyd: this.form.fjcyd,
                                fzdyzh: this.form.fzdyzh,
                                fzdyznr: this.form.fzdyznr,
                                // ffgid: this.modaObj.ffgid,
                                // fzjid: this.modaObj.fzjid
                                id: this.modaObj.id
                            }
                            updateTk(data).then(res => {
                                if(res.code == 200){
                                    this.$message.success(res.message)
                                    this.handleCancel()
                                    this.$emit('getTreeData',this.modaObj.ffgid)
                                } else {
                                    this.$message.warning(res.message)
                                }
                            })
                        } else {
                            let data = {
                                fjcyd: this.form.fjcyd,
                                fzdyzh: this.form.fzdyzh,
                                fzdyznr: this.form.fzdyznr,
                                ffgid: this.modaObj.ffgid,
                                fzjid: this.modaObj.fzjid
                            }
                            addTk(data).then(res => {
                                if(res.code == 200){
                                    this.$message.success(res.message)
                                    this.handleCancel()
                                    this.$emit('getTreeData',this.modaObj.ffgid)
                                } else {
                                    this.$message.warning(res.message)
                                }
                            })
                        }
                    } else if(this.modaObj.type == 5){
                        //console.log(this.modaObj)
                        if(this.modaObj.formData){
                            let data = {
                                fly: this.form.fly,
                                fmsmb: this.form.fmsmb,
                                fsycs: this.form.fsycs,
                                fzjid: this.modaObj.formData.fzjid,
                                id: this.modaObj.formData.id,
                            }
                            updateMsmb(data).then(res => {
                                if(res.code == 200){
                                    this.$message.success(res.message)
                                    this.handleCancel()
                                    this.$emit('getTreeData',this.modaObj.ffgid)
                                } else {
                                    this.$message.warning(res.message)
                                }
                            })
                        } else {
                            let data = {
                                fly: this.form.fly,
                                fmsmb: this.form.fmsmb,
                                fsycs: this.form.fsycs,
                                fzjid: this.modaObj.fzjid
                            }
                            addMsmb(data).then(res => {
                                if(res.code == 200){
                                    this.$message.success(res.message)
                                    this.handleCancel()
                                    this.$emit('getTreeData',this.modaObj.ffgid)
                                } else {
                                    this.$message.warning(res.message)
                                }
                            })
                        }
                    }
                } else {
                    //console.log('error submit!!');
                    return false;
                }
            });
        },
        handleCancel(){
            this.addAndEditVisible = false
            this.form = {...this.kongForm}
        }
    },
    watch: {
        modaObj(newval,oldVal){
            //console.log(newval)
            this.kongForm = JSON.parse(JSON.stringify(this.form))
            if(newval.formData){
                this.form = {
                    ...newval.formData
                }
            }
        },
        deep:true
    },
}
</script>
